package com.primb.bench.common.viewmodel;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.primb.bench.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Chen on 2017/5/10.
 * 功能描述：附件数据驱动模型
 */

public class ViewAdjunctEntity implements Parcelable {
    private List<AdjunctFileEntity> adjunctList;

    public List<AdjunctFileEntity> getAdjunctList() {
        return adjunctList;
    }

    public void setAdjunctListEntity(List<AdjunctFileEntity> adjunctList) {
        this.adjunctList = adjunctList;
    }

    public void setAdjunctListMap(List<Map<String, Object>> mapList) {
        if (adjunctList != null) {
            adjunctList = new ArrayList<>();
        } else {
            adjunctList.clear();
        }
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            AdjunctFileEntity entity = new AdjunctFileEntity(map.get("fileName").toString(), map.get("fileId").toString(), map.get("fileType").toString(), map.get("fileUniqueName").toString());
            adjunctList.add(entity);
        }
    }

    public void addView(Context context, ViewGroup viewGroup, OnFileItemClickListener mListener) {
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        layout.setGravity(Gravity.CENTER_VERTICAL);

        ImageView ivIcon = new ImageView(context);
        LinearLayout.LayoutParams paramsIcon = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ivIcon.setImageResource(R.mipmap.title_file_icon);
        ivIcon.setLayoutParams(paramsIcon);
        ivIcon.setPadding(40, 40, 0, 40);

        layout.addView(ivIcon);

        TextView tvValue = new TextView(context);
        tvValue.setText("附件信息");
        tvValue.setPadding(30, 20, 30, 20);
        tvValue.setTextSize(16);
        tvValue.setTextColor(ContextCompat.getColor(context, R.color.app_font));
        layout.addView(tvValue);

        viewGroup.addView(layout);

        RecyclerView recyclerView = new RecyclerView(context);
        recyclerView.setPadding(20, 20, 20, 20);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        BaseQuickAdapter<AdjunctFileEntity, BaseViewHolder> adapter = new BaseQuickAdapter<AdjunctFileEntity, BaseViewHolder>(R.layout.item_approval_adjunct, adjunctList) {

            @Override
            protected void convert(BaseViewHolder helper, AdjunctFileEntity item) {
                if (item.getFileType().equals("doc") || item.getFileType().equals("docx")) {
                    helper.setImageResource(R.id.iv_item_adjunct_icon, R.mipmap.adunct_doc);
                } else if (item.getFileType().equals("xls") || item.getFileType().equals("xlsx")) {
                    helper.setImageResource(R.id.iv_item_adjunct_icon, R.mipmap.adunct_excel);
                } else if (item.getFileType().equals("pdf")) {
                    helper.setImageResource(R.id.iv_item_adjunct_icon, R.mipmap.adunct_pdf);
                } else {
                    helper.setImageResource(R.id.iv_item_adjunct_icon, R.mipmap.adunct_file);
                }
                helper.setText(R.id.tv_item_adjunct_name, item.fileName);
            }
        };
        adapter.setOnItemClickListener((quickAdapter, view, position) -> {
            mListener.onItemClick(adjunctList.get(position));
        });
        recyclerView.setAdapter(adapter);
        viewGroup.addView(recyclerView);
        View line = new View(context);
        line.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_efefef));
        viewGroup.addView(line, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
    }

    public static class AdjunctFileEntity implements Parcelable {
        private String fileName;
        private String fileId;
        private String fileType;
        private String fileUniqueName;

        public String getFileUniqueName() {
            return fileUniqueName;
        }

        public void setFileUniqueName(String fileUniqueName) {
            this.fileUniqueName = fileUniqueName;
        }

        public String getFileId() {
            return fileId;
        }

        public void setFileId(String fileId) {
            this.fileId = fileId;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getFileType() {
            return fileType;
        }

        public void setFileType(String fileType) {
            this.fileType = fileType;
        }

        public AdjunctFileEntity() {
        }

        public AdjunctFileEntity(String fileId, String fileName, String fileType, String fileUniqueName) {
            this.fileId = fileId;
            this.fileName = fileName;
            this.fileType = fileType;
            this.fileUniqueName = fileUniqueName;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.fileName);
            dest.writeString(this.fileId);
            dest.writeString(this.fileType);
            dest.writeString(this.fileUniqueName);
        }

        protected AdjunctFileEntity(Parcel in) {
            this.fileName = in.readString();
            this.fileId = in.readString();
            this.fileType = in.readString();
            this.fileUniqueName = in.readString();
        }

        public static final Creator<AdjunctFileEntity> CREATOR = new Creator<AdjunctFileEntity>() {
            @Override
            public AdjunctFileEntity createFromParcel(Parcel source) {
                return new AdjunctFileEntity(source);
            }

            @Override
            public AdjunctFileEntity[] newArray(int size) {
                return new AdjunctFileEntity[size];
            }
        };
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.adjunctList);
    }

    public ViewAdjunctEntity() {
    }

    protected ViewAdjunctEntity(Parcel in) {
        this.adjunctList = in.createTypedArrayList(AdjunctFileEntity.CREATOR);
    }

    public static final Creator<ViewAdjunctEntity> CREATOR = new Creator<ViewAdjunctEntity>() {
        @Override
        public ViewAdjunctEntity createFromParcel(Parcel source) {
            return new ViewAdjunctEntity(source);
        }

        @Override
        public ViewAdjunctEntity[] newArray(int size) {
            return new ViewAdjunctEntity[size];
        }
    };

    public interface OnFileItemClickListener {
        void onItemClick(AdjunctFileEntity entity);
    }
}
